package com.example.office.controller;

import com.example.office.util.office.ExportWordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author jujh
 * @Date 2018/12/13
 */

@Controller
@Slf4j
public class EchartsController {

    @RequestMapping(value = "/viewEcharts")
    @ResponseBody
    public Map<String, Object> echartsMap(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("category", new String[]{"70年代人数", "80年代人数", "90年代人数", "00年代人数"});
        map.put("child", new int[]{10000, 20000, 30000, 40000});
        map.put("old", new int[]{30000, 25000, 20000, 15000});
        map.put("man", new int[]{1000000, 1500000, 1550000, 1600000});
        return map;
    }

    @RequestMapping("/goEchart")
    public String goEchart(HttpServletRequest request, HttpServletResponse response) {
        return "taxi/echart";
    }

    /**
     * 报警分析报表下载
     */
    @RequestMapping("/exportReport")
    @ResponseBody
    public Map<String, String> exportReport(HttpServletResponse response, HttpServletRequest request, String picBase64Info1) {
        Map<String, String> map = new HashMap();
        try {
            byte[] base64Info1 = decodeBase64(picBase64Info1);
            XWPFDocument xdoc = new ExportWordUtil().export(base64Info1);

            Calendar c = Calendar.getInstance();
            String fileName = "生成分析报告" + c.get(Calendar.YEAR) + to2String(String.valueOf((c.get(Calendar.MONTH) + 1)))
                    + c.get(Calendar.DAY_OF_MONTH) + c.get(Calendar.HOUR_OF_DAY) + c.get(Calendar.MILLISECOND) + ".docx";
            //获取存放路径
            String classPath = "D:\\temp\\";
            // String os_name = System.getProperties().get("os.name").toString().toLowerCase();
//            if (os_name.indexOf("windows") != -1) {
//                classPath = classPath.substring(1, classPath.indexOf("/WEB-INF/classes")) + "/upload/";
//            } else if (os_name.indexOf("linux") != -1) {
//                classPath = FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//                classPath = "/" + classPath.substring(1, classPath.indexOf("/WEB-INF/")) + "/upload/";
//            }
            log.info("==============report================" + classPath + fileName);
            FileOutputStream fos = new FileOutputStream(classPath + fileName);
            xdoc.write(fos);
            fos.close();

            map.put("ret", "/upload/" + fileName);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("ret", "faild");
            return map;
        }
    }


    /**
     * 解析base64，返回图片所在路径
     *
     * @param base64Info
     * @return
     */
    private byte[] decodeBase64(String base64Info) {
        if (StringUtils.isEmpty(base64Info)) {
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        if (!base64Info.contains("base64,"))
            return null;
        String[] arr = base64Info.split("base64,");
        // 数据中：
        // ... 在"base64,"之后的才是图片信息
        try {
            return decoder.decodeBuffer(arr[1]);
        } catch (IOException e) {
            log.info(e.getMessage(), e);
            return null;
        }
    }

    private String to2String(String str) {
        if (str.length() > 2) {
            str = str.substring(0, 2);
        } else {
            str = "0" + str;
        }
        return str;
    }

}
